﻿using CoreShop.App.Scale;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CoreShop.App.Test
{
    public partial class frmSafeSerialPort : Form
    {
        private SafeSerialPort _safeSerialPort;
        public frmSafeSerialPort()
        {
            InitializeComponent();
            _safeSerialPort = new SafeSerialPort("COM1", 9600);
            _safeSerialPort.DataReceived += SafeSerialPort_DataReceived;
            _safeSerialPort.ErrorReceived += SafeSerialPort_ErrorReceived;
        }
        private void SafeSerialPort_DataReceived(object sender, SerialDataEventArgs e)
        {
            // 在UI线程中处理接收到的数据
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => SafeSerialPort_DataReceived(sender, e)));
                return;
            }

            // 处理数据
            string data = System.Text.Encoding.UTF8.GetString(e.Data);
            // 更新UI
        }

        private void SafeSerialPort_ErrorReceived(object sender, SerialErrorEventArgs e)
        {
            // 处理错误
        }

        private void FormClosingHandler(object sender, FormClosingEventArgs e)
        {
            if (_safeSerialPort != null)
            {
                _safeSerialPort.Close();
                _safeSerialPort.Dispose();
            }
        }
    }
}
